#############################################################
### Replication Code: Cleaning Raw Data                   ###
### Title: Can Conservatives Be Persuaded to Support UBI? ###
### Author: Eddy S. F. Yeung                              ###
### Version: September 6, 2022                            ###
#############################################################

# Set-up ----
## Clean the R environment and set the working directory
rm(list = ls())
setwd("~/Desktop/UBI/PB_replication") # change to your own directory

## Load the required package
library(tidyverse) # version 1.3.1

## Import the datasets and merge them vertically
df_final1 <- read.csv("final1.csv")
df_final1 <- df_final1[-c(1, 2), ]
df_final2 <- read.csv("final2.csv")
df_final2 <- df_final2[-c(1, 2), ]
df <- rbind(df_final1, df_final2)
df <- df %>% subset(Finished == 1)  # drop respondents who did not complete the survey

# Recode demographic variables ----
## Age
df$age <- as.numeric(df$age) + 17

## Sex (1 = female)
df$female <- ifelse(df$sex == "2", 1, ifelse(df$sex == "1", 0, NA))

## Marital status (1 = married)
df$married <- ifelse(df$marital == "2", 1, ifelse(is.na(df$marital) == F, 0, NA))

## Education (0 = less than high school; 7 = doctorate degree)
df$educ <- as.numeric(df$educ) - 1

## Income (0 = less than $10,000; 13 = $150,000 or more)
df$income <- as.numeric(df$income) - 1

## Race
df$white <- ifelse(df$race == "1", 1,  # (1 = White)
                   ifelse(df$race == "2" | df$race == "3" | 
                          df$race == "4" | df$race == "5" |
                          df$race == "6" | df$race == "7", 0, NA))
df$black <- ifelse(df$race == "2", 1,  # (1 = Black)
                   ifelse(df$race == "1" | df$race == "3" | 
                          df$race == "4" | df$race == "5" |
                          df$race == "6" | df$race == "7", 0, NA))

## Employment status
df$employ <- ifelse(df$employ == "1" | df$employ == "2", 0,  # (0 = employed)
                    ifelse(df$employ == "3", 1,              # (1 = not employed)
                    ifelse(df$employ == "4", 2, NA)))        # (2 = retired)
df$employ <- as.factor(df$employ)

## Household union membership (1 = yes)
df$union <- ifelse(df$union == "1", 1, ifelse(df$union == "2", 0, NA))

## Party identification (0 = strong Democrat; 6 = strong Republican)
df$pid <- ifelse(df$partyid.2d == "1", 0,
                 ifelse(df$partyid.2d == "2", 1,
                 ifelse(df$partyid.2n == "2", 2,
                 ifelse(df$partyid.2n == "3", 3,
                 ifelse(df$partyid.2n == "1", 4,
                 ifelse(df$partyid.2r == "2", 5,
                 ifelse(df$partyid.2r == "1", 6, NA)))))))

## Current recipient of any means-tested welfare programs in the US (1 = yes)
df$welfare.receive <- ifelse(df$welfare.parti == "1", 1, 
                             ifelse(df$welfare.parti == "2", 0, NA))

# Recode attitudinal variables ----
## View on current welfare system in the US (1 = not well administered)
df$welfare.bad <- ifelse(df$welfare.eval == "2", 1, 
                         ifelse(df$welfare.eval == "1", 0, NA))

## Racial resentment (0 = lowest; 8 = highest)
df$resent1 <- as.numeric(df$resent1) - 1
df$resent2 <- as.numeric(df$resent2) - 1
df$racial.resent <- df$resent1 + df$resent2

## Perceived demographic change in surrounding environment (1 = yes)
df$demo.change <- ifelse(df$envir == "1", 1, ifelse(df$envir == "2", 0, NA))

# Recode other variables (treatment dummies, DVs, attention checks) ----
## Experimental group
df$treatment1 <- ifelse(df$group == "1", 1, 0) # (1 = equalizing opportunity)
df$treatment2 <- ifelse(df$group == "2", 1, 0) # (1 = limiting government)

## Support for UBI (0 = strongly against; 6 = strongly in favor)
df$support <- as.numeric(df$support) - 1

## Percentage support for UBI (0 = do not support; 1 = support)
df$percent.sup <- ifelse(df$support >= 4 & df$support <= 6, 1,
                         ifelse(df$support >= 0 & df$support <= 3, 0, NA))

## Association of UBI with Democratic politicians
df$asso.UBI <- ifelse(df$asso.UBI == "1", 1, 0)

## Association of TANF with Democratic politicians
df$asso.TANF <- ifelse(df$asso.TANF == "1", 1, 0)

## Attention (0 = least attentive; 2 = most attentive)
df$screener1 <- ifelse(df$screener1 == "", 1, 0)
df$screener2 <- ifelse(df$screener2 == "3,5", 1, 0)
df$attention <- df$screener1 + df$screener2

# Create the conservatism score and identify conservatives ----
## Conservatism score (0 = most liberal; 10 = most conservative)
df$policy1 <- ifelse(df$policy1 == "2", 1, 0)
df$policy2 <- ifelse(df$policy2 == "1", 1, 0)
df$policy3 <- ifelse(df$policy3 == "2", 1, 0)
df$policy4 <- ifelse(df$policy4 == "2", 1, 0)
df$policy5 <- ifelse(df$policy5 == "1", 1, 0)
df$policy6 <- ifelse(df$policy6 == "2", 1, 0)
df$policy7 <- ifelse(df$policy7 == "2", 1, 0)
df$policy8 <- ifelse(df$policy8 == "2", 1, 0)
df$policy9 <- ifelse(df$policy9 == "2", 1, 0)
df$policy10 <- ifelse(df$policy10 == "2", 1, 0)
df$cons.score <- 
  df$policy1 + df$policy2 + df$policy3 + df$policy4 + df$policy5 +
  df$policy6 + df$policy7 + df$policy8 + df$policy9 + df$policy10

## Conservative (1 = yes)
df$conservative  <- ifelse(df$cons.score >= 5, 1, 0)  # threshold at 5
df$conservative6 <- ifelse(df$cons.score >= 6, 1, 0)  # threshold at 6
df$conservative7 <- ifelse(df$cons.score >= 7, 1, 0)  # threshold at 7

# Create the economic conservatism score and identify economic conservatives ----
## Economic conservatism score (0 = most liberal; 6 = most conservative)
df$e.cons.score <- 
  df$policy1 + df$policy4 + df$policy6 + df$policy8 + df$policy9 + df$policy10

## Economic conservative (1 = yes)
df$e.conservative <- ifelse(df$e.cons.score >= 3, 1, 0)

## Social conservatism score (0 = most liberal; 4 = most conservative)
df$s.cons.score <- df$policy2 + df$policy3 + df$policy5 + df$policy7

## Social conservative (1 = yes)
df$s.conservative <- ifelse(df$s.cons.score >= 2, 1, 0)

# Export the cleaned dataset as an RData file ----
save(df, file = "cleaned.RData")